<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: UserAddress.php
 * Date: 2017/1/5
 * Time: 23:24
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;
use app\library\Flag;

class UserAddress extends Model
{
    //set talbename
    protected $table = 'user_address';
    //set primary key
    protected $pk = 'address_id';

    public function getAddressById($address_id)
    {
        $conds = array(
            'address_id' => ['=', $address_id],
        );
        $field = 'create_time,update_time';
        return Db::table($this->table)->where($conds)->field($field, true)->find();
    }


    /**
     * @param $user_id
     * @return array|false|\PDOStatement|string|Model
     * 查询结果不存在时返回null
     */
    public function getAddressByUserid($user_id)
    {
        $conds = array(
            'user_id' => ['=', $user_id],
            'status' => ['=', Flag::ADDRESS_STATUS_ACTIVE],
        );
        $field = 'create_time, update_time';
        $data = $this->where($conds)->field($field, true)->select();//返回包含object的数组
        return $data;
        //return Db::table($this->table)->where($conds)->field($field,true)->select();
    }


    //public function getIsDefaultAttr($value){
    //    $status = [0=>'默认',1=>'非默认'];
    //    return $status[$value];
    //}

    /**
     * @param $param
     * @return false|int
     */
    public function addAddress($param)
    {

        $param['create_time'] = time();
        $param['update_time'] = time();
        $this->data($param);
        return $this->allowField(true)->save();
    }

    /**
     * @param $address_id
     * @return int|string
     * @throws \think\Exception
     */
    public function removeAddress($user_id, $address_id)
    {
        $conds = array(
            'user_id' => $user_id,
            'address_id' => $address_id,
        );
        $field = array(
            'status' => Flag::ADDRESS_STATUS_DELETED,
        );
        return $this->where($conds)->update($field);
    }

    /**
     * @param $param
     * @return false|int
     */
    public function updateAddress($param)
    {
        $conds = array(
            'address_id' => $param['address_id'],
        );
        $field = array();
        foreach ($param as $key => $value) {
            if ($key == 'address_id') {
                continue;
            }
            $field[$key] = $value;
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @param $address_id
     * @param $user_id
     * @return bool
     * @DESC 设置默认地址ַ
     */
    public function setDefaultAddress($address_id, $user_id)
    {

        Db::startTrans();
        try {
            $arrUserAddress = Db::table($this->table)->where(['user_id' => $user_id])->select();
            foreach ($arrUserAddress as $arr) {
                if ($arr['is_default'] == Flag::ADDRESS_DEFAULT) {
                    Db::table($this->table)->where(['address_id' => $arr['address_id']])->setField(['is_default' => Flag::ADDRESS_NOT_DEFAULT]);
                }
            }
            Db::table($this->table)->where(['address_id' => $address_id])->setField(['is_default' => Flag::ADDRESS_DEFAULT]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }

}

